import time
from Common.basepage import BasePage
from Common.handle_log import do_log
from PageLocators.MainPage_locator import MainPageLocator as loc
from Common.connect_to_device import *

class MainPage(BasePage):

    def click_home(self):
        if device_type=="A1Back":
            d_one_device.shell("input keyevent 3")
            do_log.info(f"通过adb回到桌面退出")
        else:
            height, width = d_one.window_size()
            # 1600 2560
            # 这里是横屏，所以反向
            d_one.click(width * 0.02, height * 0.94)
            do_log.info("点击左下角的首页按钮")

    def click_home_byadb(self):
        d_one_device.shell("input keyevent 3")
        do_log.info(f"通过adb回到桌面退出")
    def click_car(self):
        height, width = d_one.window_size()
        # 1600 2560
        # 这里是横屏，所以反向
        d_one.click(width * 0.5, height * 0.5)
        time.sleep(1)
        d_one.click(width * 0.101, height * 0.95)
        do_log.info("车机下方中控设置_点击车机设置(汽车图标)")
    def click_air_conditioner_loc(self):
        try:
            eval(self.d).swipe(1400, 1600, 1400, 1400, 0.2)
            do_log.info("点击 {} 坐标点1400, 1600, 1400, 1400成功！")
            time.sleep(0.5)
            # d_one(resourceId = "com.zeekr.launcher.rear:id/window_navigation_hvac").click()
            eval(self.d).click(1732, 1482)
            do_log.info("点击正下方空调按钮成功")

        except:
            do_log.exception("点击 {} 坐标点{},{},{},{}失败！")
            self.save_img("上滑失败")
            raise
    # def click_home(self):
    #     self.click_element(loc.home_loc, "点击左下角的首页按钮")
    #     do_log.info("点击左下角的首页按钮")

    def swipe_to_GEEKR_cinema(self):
        if device_type != "A1Back":
            height, width = d_one.window_size()
            # 这里是横屏，所以反向
            d_one.swipe(width * 0.996, height // 2, 0, height // 2)
            do_log.info("左滑页面到极氪影院")
            try:
                self.click_element(loc.GEEKR_Cinema, "主页_点击5D专区")
            except:
                self.click_point(940, 197, "极氪影院")

        else:
            try:
                self.click_element(loc.GEEKR_Cinema, "主页_点击5D专区")
            except:
                self.click_point(940, 197, "极氪影院")



    def enter_film1(self, filmname):
        self.click_element(loc.GEEKR_Cinema, "主页_点击5D专区")
        time.sleep(1)
        film_loc = loc.film_loc.replace("#film_name#", filmname)
        count = 0
        max_count = 3
        while not self.judge_ele_exists(film_loc) and count < max_count:
            self.swipe("up", "5D专区向上滑动屏幕")
            time.sleep(5)
            count += 1
        self.click_element(film_loc, "点击 {} 电影".format(filmname))
    def enter_film(self, filmname):
        # film_index={
        #     "封神第一部：朝歌风云":[2750,1342],
        #     "冰雪奇缘": [914,967],
        #     "寻梦环游记": [1437, 1488],
        #     "哥斯拉大战金刚": [1961, 1461],
        #     "封神第一部：朝歌风云": [2750, 1342]
        #             }

        count = 0
        film_loc = loc.film_loc.replace("#film_name#", filmname)
        film_loc_fsdyb = loc.film_loc.replace("#film_name#", "哪吒之魔童降世")
        film_loc_xlql = loc.film_loc.replace("#film_name#", "心灵奇旅")
        time.sleep(2)
        if self.judge_ele_exists(film_loc):
            self.click_element(film_loc, "点击 {} 电影".format(filmname))
            return None
        self.swipe_to_GEEKR_cinema()
        if device_type == "A1Back":
            while not self.judge_ele_exists(film_loc) and count < 5:
                MainPage(device="d_one").click_home_byadb()
                time.sleep(1)
                MainPage(device="d_one").swipe_to_GEEKR_cinema()
                time.sleep(1)
                d_one.swipe(1500, 600, 1500, 700, duration=0.01)
                time.sleep(0.5)
                d_one.swipe(1500, 600, 1500, 700, duration=0.01)
                time.sleep(0.5)
                d_one.swipe(1500, 800, 1500, 500, duration=0.03)
                time.sleep(0.5)
                count_min = 0
                while not self.judge_ele_exists(film_loc) and count_min < 4:
                    d_one.swipe(1500, 1275, 1500, 644, duration=0.3)
                    do_log.info("目标影片不在当前页面上，进行上滑第{}次操作".format(count_min + 1))
                    time.sleep(1)
                    count_min += 1
                time.sleep(1)
                count += 1
            time.sleep(1)
            while self.judge_ele_exists(film_loc):
                self.click_element(film_loc, "点击 {} 电影".format(filmname))
        else:
            d_one.swipe(1500, 600, 1500, 700, duration=0.02)
            time.sleep(0.5)
            d_one.swipe(1500, 800, 1500, 500, duration=0.1)
            while not self.judge_ele_exists(film_loc) and count < 5:
                d_one.swipe(1500, 800, 1500, 500, duration=0.1)
                if not self.judge_ele_exists(film_loc) and self.judge_ele_exists(film_loc_fsdyb):
                    d_one.swipe(1500, 900, 1500, 600, duration=0.03)
                    do_log.info("哪吒之魔童降世在页面上，进行上滑操作")
                if not self.judge_ele_exists(film_loc) and self.judge_ele_exists(film_loc_xlql):
                    d_one.swipe(1500, 600, 1500, 900, duration=0.03)
                    do_log.info("心灵奇旅在页面上，进行下滑操作")
                time.sleep(1)
                count += 1
            self.click_element(film_loc, "点击 {} 电影".format(filmname))


        do_log.info("进入影院成功")

    def click_conditioner(self):
        self.click_element(loc.conditioner_loc, "点击空调设置")
        do_log.info("点击空调设置")
    def click_conditioner(self):
        self.swipe()
        do_log.info("点击空调设置")


if __name__ == '__main__':
    # MainPage(device="d_one").click_home_byadb()
    # MainPage(device="d_one").swipe_to_GEEKR_cinema()
    # time.sleep(1)
    # d_one.swipe(1500, 1000, 1500, 800, duration=0.01)
    # time.sleep(0.5)
    # 冰雪奇缘
    # "封神第一部：朝歌风云": [2750, 1342],
    # "冰雪奇缘": [914, 967],
    # "寻梦环游记": [1437, 1488],
    # "哥斯拉大战金刚": [1961, 1461],
    # "封神第一部：朝歌风云": [2750, 1342],
    MainPage(device="d_one").click_home()





